package com.lbn.monitor.app.warning;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.free.framework.ui.FjrAppbar;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.AlarmDevice;
import com.lbn.monitor.app.warning.adapter.WarningAdapter;
import com.lbn.monitor.base.AppBaseActivity;
import com.lbn.monitor.util.String2Num;
import com.lbn.monitor.widget.Appbar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrFrameLayout;
import indi.free.widgets.custom_recycler_view.BasePagerRecyclerAdapter;
import indi.free.widgets.custom_recycler_view.BaseRecyclerAdapter;

public class WarningActivity extends AppBaseActivity<WarningPresenter> implements IWarningView<List<AlarmDevice>>,
        AppBaseActivity.OnPtrRefresh, BasePagerRecyclerAdapter.OnLoadMoreListener, BasePagerRecyclerAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    WarningAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warning);
        ButterKnife.bind(this);
        setAppbar(new Appbar(this)
                .setTitle("报警设备列表")
                .openBack());
//        setAppbar(new FjrAppbar.Builder(this)
//                .title("设备列表")
//                .titleInCenter(true)
//                .create());
    }


    @Override
    protected WarningPresenter createPresenter() {
        return new WarningPresenter();
    }


    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, WarningActivity.class));
    }

    @Override
    protected void onPresenterCreated() {
        super.onPresenterCreated();
        adapter = new WarningAdapter(this, recyclerView, null);
        adapter.setOnItemClickListener(this);
        adapter.setmOnLoadMoreListener(this);
        recyclerView.setAdapter(adapter);
        initPtr(ptrFrame, this);
    }

    int page = 1;

    @Override
    public void onPtrRefresh() {
        page = 1;
        mPresenter.warningList(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.warningList(page);
    }

    @Override
    public void refreshComplete(List<AlarmDevice> data) {
        adapter.refreshData(data, true);
        ptrFrame.refreshComplete();
    }

    @Override
    public void addMoreData(List<AlarmDevice> data, boolean isMore) {
        adapter.addMoreData(data, isMore);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        AlarmDevice data = adapter.getItem(pos);
        WHistoryActivity
                .startActivity(
                        this,
                        data.getTreename(),
                        data.getDeviceMN(),
                        data.getPollutantsFactor(),
                        data.getStandardvalue(),
                        data.getDataTime());
    }
}
